package com.woniuxy.service.impl;

import com.woniuxy.entity.User;
import com.woniuxy.entity.UserExample;
import com.woniuxy.mapper.UserMapper;
import com.woniuxy.service.UserService;
import com.woniuxy.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Map<String, Object> selectUserNowCampOrCity(int userId) {
        Map<String, Object> map = userMapper.selectUserNowCampOrCity(userId).get(0);
        return map;
    }

    @Override
    public UserVO login(String userName,String password) throws Exception {
        UserVO user = userMapper.selectUserByUserNameOrPassword(userName);
        if(user!=null && user.getPassword().equals(password)){
            return user;
        }
        return null;
    }

    @Override
    public int insertUser(User user) throws Exception {
        return userMapper.insertSelective(user);
    }

    @Override
    public String selectTel(String tel) throws Exception {
        if (userMapper.selectTel(tel)==null){
            return null;
        }
        return userMapper.selectTel(tel);
    }
    //用户更新头像
    @Override
    public int updateUserImgUrl(User user) {
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public int updateUserAccount(Map<String, Object> map) throws Exception {
        return userMapper.updateUserAccount(map);
    }

    @Override
    public User selectUserByUserId(Integer userId) throws Exception {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        List<User> users = userMapper.selectByExample(example);
        return users.size()>0?users.get(0):null;
    }

}
